package com.gopay.common.riskcontrol.dao.impl;

import com.gopay.common.base.dao.impl.BaseReadDAOImpl;
import com.gopay.common.cipher.utils.SensitiveInfoUtils;
import com.gopay.common.domain.riskcontrol.RcListing;
import com.gopay.common.riskcontrol.dao.RcListingQueryDAO;
import com.gopay.common.util.FastJsonUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * @RcListingQueryDAOImpl.java
 * @author xuan
 * @2016年1月19日 下午6:11:28 www.gopay.com.cn Inc.All rights reserved.
 */
@Repository("rcListingQueryDAO")
public class RcListingQueryDAOImpl extends BaseReadDAOImpl<RcListing, Long>implements RcListingQueryDAO {
	/**
	 * 定义日志对象
	 */
	private static final Logger logger = LoggerFactory.getLogger(RcListingQueryDAOImpl.class);
	@Override
	public RcListing queryRcListing(String custId, String type, String stat) {
		/**
		 * 定义查询SQL
		 */
		StringBuffer hql = new StringBuffer("FROM RcListing where custId=:custId and type=:type");
		
		if(StringUtils.isNotBlank(stat)){
			hql.append(" and stat=:stat");
		}
		/**
		 * 封装查询条件
		 */
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("custId", custId);
		param.put("type", type);
		if(StringUtils.isNotBlank(stat)){
			param.put("stat", stat);
		}
		List<RcListing> resultList=null;
		try{
			resultList = this.findByHql(hql.toString(), param);
		}catch(Exception e){
			logger.info("根据用户ID查询记录出现问题："+e.getMessage());
		}
		if (resultList == null || resultList.size() == 0) {
			return null;
		}
		return resultList.get(0);
	}

	/**
	 * @Description 根据条件查询风险名单
	 * @Author jianghp
	 * @Date 2017/7/5 9:20
	 * Copyright (c) 2017. www.gopay.com.cn Inc.All rights reserved.
	 */
	@Override
	public List<RcListing> queryRcListingByCondition(Map map) {
		Map<String, Object> para = new HashMap<String, Object>();
		StringBuffer hql = new StringBuffer("FROM RcListing where 1=1");
		if(map.get("custType")!=null && StringUtils.isNotBlank(map.get("custType").toString())){
			para.put("custType",map.get("custType"));
			hql.append(" and custType=:custType");
		}
		if(map.get("stat")!=null && StringUtils.isNotBlank(map.get("stat").toString())){
			para.put("stat",map.get("stat"));
			hql.append(" and stat=:stat");
		}
		if(map.get("type")!=null && StringUtils.isNotBlank(map.get("type").toString())){
			para.put("type",map.get("type"));
			hql.append(" and type=:type");
		}
		if(map.get("mobile")!=null && StringUtils.isNotBlank(map.get("mobile").toString()) && map.get("certNo")!=null && StringUtils.isNotBlank(map.get("certNo").toString()) ){
			//pci 加密查询处理
			para.put("mobile", SensitiveInfoUtils.getAllHisCipherText(map.get("mobile").toString()).toArray());
			para.put("certNo", SensitiveInfoUtils.getAllHisCipherText(map.get("certNo").toString()).toArray());
			hql.append(" and (emailMob in(:mobile) or certNo in(:certNo))");
		}else if(map.get("mobile")!=null && StringUtils.isNotBlank(map.get("mobile").toString()) && map.get("certNo")==null){
			//pci 加密查询处理
			para.put("mobile", SensitiveInfoUtils.getAllHisCipherText(map.get("mobile").toString()).toArray());
			hql.append(" and emailMob in(:mobile)");
		}else if(map.get("mobile")==null && map.get("certNo")!=null && StringUtils.isNotBlank(map.get("certNo").toString())){
			//pci 加密查询处理
			para.put("certNo", SensitiveInfoUtils.getAllHisCipherText(map.get("certNo").toString()).toArray());
			hql.append(" and certNo in(:certNo)");
		}

		List<RcListing> resultList=null;
		try{
			logger.info("根据条件查询风险名单参数："+ FastJsonUtil.toJSONString(para)+";HQL:"+hql.toString());
			if(para.size()==0) {
                return null;
            }
			resultList = this.findByHql(hql.toString(), para);
		}catch(Exception e){
			logger.info("根据条件查询风险名单记录出现问题："+e.getMessage());
		}
		if (resultList == null || resultList.size() == 0) {
			return null;
		}
		return resultList;
	}

}